package com.infocollect.project.api.ui;


import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.TappFromTempEntity;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.helper.MapHelper;
import com.infocollect.project.api.service.ITappFromTempService;
import com.infocollect.project.api.vo.CurrentUserInfoVO;
import com.infocollect.project.api.vo.FromTempContextVO;
import com.infocollect.project.form.vo.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(value = "FromTemp", tags = "表单模块信息")
@RestController
@RequestMapping("FromTemp")
public class TappFromTempController {
    @Autowired
    private TokenService tokenService;
    /**
     * 服务对象
     */
    @Resource
    private ITappFromTempService tappFromTempService;
    /**
     * 保存表单信息
     *
     * @param
     * @return
     * @throws IOException
     */
    @PostMapping("SaveData")
    @ResponseBody
    public R<Long> SaveData(@RequestBody Map<String, String> param) throws IOException {
        if (param.get("FromName") == null || param.get("FromName").length() == 0) {
            return R.newError("页面名称不能为空！");
        }
        try {
            TappFromTempEntity entity = (param.get("fromID") == null || param.get("fromID").length() == 0) ? null : this.tappFromTempService.queryById(Long.parseLong(param.get("fromID")));
//            CurrentUserInfoVO currentUserInfo = CurrentHelp.Ins.GetUser();
            LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
            if (entity == null) {
                entity = new TappFromTempEntity();
                entity.setFcreateby(currentUserInfo.getUserName());
                entity.setFlastupdateby(currentUserInfo.getUserName());
                entity.setFcreationdate(new Date());
                entity.setFlastupdatedate(new Date());
                entity.setFpagecontroljson(param.get("PageControl"));
                entity.setFromcode(param.get("FromCode"));
                entity.setFremarks(param.get("FRemarks"));
                entity.setFromcontent(param.get("FromTempContext"));
                entity.setFromdesignjson(param.get("FromTempJson"));
                entity.setFromname(param.get("FromName"));
                entity.setNodepower(param.get("NodePower"));
                entity.setDefaultdata(param.get("defaultdata"));
                entity.setControltitle(param.get("controlTitle"));
                entity = this.tappFromTempService.insert(entity);
            } else {
                entity.setFlastupdateby(currentUserInfo.getUserName());
                entity.setFlastupdatedate(new Date());
                entity.setFpagecontroljson(param.get("PageControl"));
                entity.setFromcode(param.get("FromCode"));
                entity.setFremarks(param.get("FRemarks"));
                entity.setFromcontent(param.get("FromTempContext"));
                entity.setFromdesignjson(param.get("FromTempJson"));
                entity.setFromname(param.get("FromName"));
                entity.setNodepower(param.get("NodePower"));
                entity.setDefaultdata(param.get("defaultdata"));
                entity.setControltitle(param.get("controlTitle"));
                entity = this.tappFromTempService.update(entity);
            }

            return R.newOk(entity.getFid());
        } catch (Exception ex) {
            return R.newError("保存报错！具体错误信息为：" + ex);
        }

    }
    /**
     * 查询流程列表
     *
     * @param param post过来的参数
     * @return 单条数据
     */
    @PostMapping("queryPageList")
    public R<List<TappFromTempEntity>> queryPageList(@RequestBody Map<String, String> param) throws Exception {
        TappFromTempEntity tappFromTemp = new TappFromTempEntity();
        MapHelper.setObjectValue(tappFromTemp, param);
        int page = Integer.parseInt(param.get("page"));
        int rows = Integer.parseInt(param.get("rows"));
        PageHelper.startPage(page, rows);
        List<TappFromTempEntity> list = tappFromTempService.querypageList(tappFromTemp);
        PageInfo pageInfo = new PageInfo(list);
        return R.newOk(list,(int)pageInfo.getTotal());

    }
    /**
     * 获取表单模板
     *
     * @return
     */
    @PostMapping("GetFromDesignJson")
    @ResponseBody
    public   R<String> GetFromDesignJson(@RequestBody Map<String, String> param) throws IOException {
        String fromID = param.get("fromID");
        if (fromID == null || fromID.length() == 0) {
            return R.newError("传入参数失败");
        }
        return R.newOk( tappFromTempService.GetFromDesignJson(Long.parseLong(fromID)));
    }

    @PostMapping("GetFromTempContext")
    @ResponseBody
    public R<FromTempContextVO> GetFromTempContext(@RequestParam("FromTempID") String fromID) throws IOException {
        if (fromID == null || fromID.length() == 0) {
            return R.newError("传入参数失败");
        }
        TappFromTempEntity tappFromTemp= this.tappFromTempService.queryById(Long.parseLong(fromID));
        if(tappFromTemp==null){
            return R.newError("找不到模板页！");
        }
        FromTempContextVO resut=new FromTempContextVO();
        resut.setFromtempcontext(tappFromTemp.getFromcontent());
        resut.setFromid(tappFromTemp.getFid());
        resut.setFromname(tappFromTemp.getFromname());
        resut.setNodepowerjson(tappFromTemp.getNodepower());
        return R.newOk( resut);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @PostMapping("GetFromTempJson")
    @ResponseBody
    public  R<TappFromTempEntity> selectOne(@RequestBody Map<String, String> param) throws IOException {
        String fromID = param.get("fromID");
        if (fromID == null || fromID.length() == 0) {
            return R.newError("传入参数失败");
        }
        return R.newOk( this.tappFromTempService.queryById(Long.parseLong(fromID)));
    }
}
